﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YunSpace.Application.SystemManage;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.Entity.SiteMgr;
using YunSpace.Domain.Entity.SystemManage;
using YunSpace.Domain.IRepository.SiteMgr;
using YunSpace.Domain.ViewModel.SiteMgr;
using YunSpace.Repository.SiteMgr;

namespace YunSpace.Application.SiteMgr
{
    public class SiteClientsApp
    {
        private ISiteClientsRepository clientService = new SiteClientsRepository();
        private ISiteClientsProjectRepository projectService = new SiteClientsProjectRepository();
        private ItemsDetailApp itemsDetailApp = new ItemsDetailApp();
        //public List<SiteClientsEntity> GetList(Pagination pagination, string userId) {
        //    var result = new List<SiteClientsEntity>();
        //    using (var db = new RepositoryBase().BeginTrans()) {
        //        var strSql = @"select * from Site_Clients where 1=1 {0} order by F_LastModifyTime desc";
        //        DbParameter[] parameter =
        //        {
        //            new MySqlParameter("@F_CreatorUserId", userId),
        //        };
        //        strSql = string.Format(strSql, userId.IsEmpty() ? "" : "and F_CreatorUserId= @F_CreatorUserId");
        //        result = db.FindList<SiteClientsEntity>(strSql, parameter, pagination);
        //    }
        //    return result;
        //}

        //public List<SiteClientsEntity> GetListForLeader(Pagination pagination,string queryJson)
        //{
        //    var result = new List<SiteClientsEntity>();
        //    using (var db = new RepositoryBase().BeginTrans())
        //    {
        //        var strSql = @"select a.F_Id,a.F_ClientName,a.F_CityId,a.F_ClientType,a.F_ClientLevel,a.F_DeleteMark,a.F_CreatorTime,b.F_RealName F_CreatorUserId,a.F_LastModifyTime
        //                    ,a.F_LastModifyUserId,a.F_DeleteTime,a.F_DeleteUserId,a.F_DepartmentId from Site_Clients a 
        //                    inner join Sys_User b on a.F_CreatorUserId=b.F_Id where 1=1 {0} {1} {2} order by a.F_CreatorUserId desc,a.F_CreatorTime desc";
        //        var queryParam = queryJson.ToJObject();
        //        DbParameter[] parameter =
        //        {
        //            new MySqlParameter("@F_Id", queryParam["UserId"]),
        //            new MySqlParameter("@F_ClientLevel",queryParam["F_ClientLevel"]),
        //            new MySqlParameter("@F_ClientType",queryParam["F_ClientType"])
        //        };
        //        strSql = string.Format(strSql, queryParam["UserId"].IsEmpty() ? "" : " and b.F_Id=@F_Id", queryParam["F_ClientLevel"].IsEmpty() ? "" : " and a.F_ClientLevel=@F_ClientLevel", queryParam["F_ClientType"].IsEmpty() ? "" : " and a.F_ClientType=@F_ClientType");
        //        result = db.FindList<SiteClientsEntity>(strSql, parameter, pagination);
        //    }
        //    return result;
        //}
        public List<SiteClientsProjectActionModel> GetIndustryRecordFor(Pagination pagination, string queryJson)
        {
            var result = new List<SiteClientsProjectActionModel>();
            using (var db = new RepositoryBase().BeginTrans())
            {
                var strSql = @"select a.F_Id,b.F_ClientName,a.F_IndustryId,a.F_ProjectName,a.F_ActivityType,a.F_ActivityBudget,a.F_Contract,
                            a.F_Department,a.F_PositionId,a.F_Phone,a.F_Mail,a.F_LastModifyTime 
                            from Site_Clients_Project a
                            inner join Site_Clients b on a.F_ClientId=b.F_Id
                            inner join Sys_User c on a.F_CreatorUserId=c.F_Id
                            where 1=1 {0}";
                var queryParam = queryJson.ToJObject();
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_FarthId", queryParam["F_FarthId"])                   
                };
                strSql = string.Format(strSql, queryParam["F_FarthId"].IsEmpty() ? "" : " and b.F_Id=@F_FarthId");
                result = db.FindList<SiteClientsProjectActionModel>(strSql, parameter, pagination);
            }
            return result;
        }

        public List<ClientActionModel> GetClientList()
        {
            var result = new List<ClientActionModel>();
            using (var db = new RepositoryBase().BeginTrans())
            {
                var strSql = @"select a.F_Id,a.F_ClientName from Site_Clients a";
                result = db.FindList<ClientActionModel>(strSql);
            }
            return result;
        }

        public List<SiteClientsProjectActionModel> GetIndustryRecord(Pagination pagination, string queryJson,string userId) {
            var result = new List<SiteClientsProjectActionModel>();
            using (var db = new RepositoryBase().BeginTrans()) {
                var strSql = @"select a.F_Id,b.F_ClientName,a.F_IndustryId,a.F_ProjectName,a.F_ActivityType,a.F_ActivityBudget,a.F_Contract,
                            a.F_Department,a.F_PositionId,a.F_Phone,a.F_Mail,a.F_LastModifyTime 
                            from Site_Clients_Project a
                            inner join Site_Clients b on a.F_ClientId=b.F_Id
                            inner join Sys_User c on a.F_CreatorUserId=c.F_Id
                            where 1=1 {0} {1}";
                var queryParam = queryJson.ToJObject();
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_FarthId", queryParam["F_FarthId"]),
                    new MySqlParameter("@F_Id", userId)
                };
                strSql = string.Format(strSql, queryParam["F_FarthId"].IsEmpty()?"": " and b.F_Id=@F_FarthId", userId.IsEmpty()?"": " and c.F_Id=@F_Id");
                result = db.FindList<SiteClientsProjectActionModel>(strSql, parameter, pagination);
            }
            return result;
        }

        public List<SiteClientProjectActionModel> GetClientHistoryRecord(Pagination pagination, string queryJson, string userId) {
            var result = new List<SiteClientProjectActionModel>();
            using (var db = new RepositoryBase().BeginTrans())
            {
                var strSql = @"select a.F_Id,b.F_ClientName,a.F_IndustryId,a.F_ProjectName,a.F_ActivityType,a.F_ActivityBudget,a.F_Contract,
                            a.F_Department,a.F_PositionId,a.F_Phone,a.F_Mail,a.F_LastModifyTime 
                            from Site_Clients_Project a
                            inner join Site_Clients b on a.F_ClientId=b.F_Id
                            inner join Sys_User c on b.F_CreatorUserId=c.F_Id
                            where 1=1 {0} {1}";
                var queryParam = queryJson.ToJObject();
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_FarthId", queryParam["F_FarthId"]),
                    new MySqlParameter("@F_Id", userId)
                };
                strSql = string.Format(strSql, queryParam["F_FarthId"].IsEmpty() ? "" : " and b.F_Id=@F_FarthId", userId.IsEmpty() ? "" : " and c.F_Id=@F_Id");
                result = db.FindList<SiteClientProjectActionModel>(strSql, parameter, pagination);
            }
            return result;
        }

        public List<ItemsDetailActionModel> GetItemList(string enCode)
        {
            return itemsDetailApp.GetItemList(enCode);
        }

        public List<UserInfoActionModel> GetUsersJson() {
            var result = new List<UserInfoActionModel>();
            using (var db = new RepositoryBase().BeginTrans())
            {
                var strSql = @"select a.F_Id,a.F_RealName from Sys_User a 
                                inner join Sys_Organize b on a.F_DepartmentId=b.F_Id where b.F_EnCode='Sales'";
                DbParameter[] parameter ={ };
                result = db.FindList<UserInfoActionModel>(strSql);
            }
            return result;
        }

        //public void SubmitForm(SiteClientsEntity entity, string keyValue)
        //{
        //    if (!string.IsNullOrEmpty(keyValue))
        //    {
        //        entity.Modify(keyValue);
        //        clientService.Update(entity);
        //    }
        //    else {
        //        entity.Create();
        //        clientService.Insert(entity);
        //    }
        //}

        public void SubmitProjectForm(SiteClientsProjectEntity entity, string keyValue) {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                projectService.Update(entity);
            }
            else
            {
                entity.Create();                
                projectService.Insert(entity);
            }
        }

        public SiteClientsProjectEntity GetProjectEntity(string keyValue) {
            return projectService.FindEntity(keyValue);
        }

        //public SiteClientsEntity GetEntity(string keyValue) {
        //    return clientService.FindEntity(keyValue);
        //}

        //public void DeleteEntity(string keyValue) {
        //    SiteClientsEntity entity= clientService.FindEntity(keyValue);
        //    clientService.Delete(entity);
        //}

        public void DeleteProjectEntity(string keyValue) {
            SiteClientsProjectEntity entity = projectService.FindEntity(keyValue);
            projectService.Delete(entity);
        }
    }
}
